﻿@page "/tests/datagrid/group/multiple"
@using Blazorise.Extensions;
<Row>
    <Column>
        <Card Margin="Margin.Is4.OnY">
            <CardHeader>
                <CardTitle>Datagrid: Groupable</CardTitle>
            </CardHeader>
            <CardBody>
                <Paragraph>
                    The <code>Groupable</code> feature, allows you to display your data in groups.
                </Paragraph>

                <Paragraph>
                    In this example, we group the data by multiple columns, <code>Gender</code> & <code>Active</code>.
                </Paragraph>

                <Paragraph>
                    <Button Color="Color.Primary" Clicked="@(() => dataGridRef.ExpandAllGroups())">Expand All</Button>
                    <Button Color="Color.Secondary" Clicked="@(() => dataGridRef.CollapseAllGroups())">Collapse All</Button>
                </Paragraph>

                <Paragraph>
                    <Field>
                        <FieldLabel>
                            <SelectList Data="@dataGridRef.DisplayGroupedData"
                                        TItem="GroupContext<Employee>" TValue="string"
                                        TextField="x=> x.Key" ValueField="x=> x.Key"
                                        @bind-SelectedValue="_selectedGroupKey"></SelectList>
                        </FieldLabel>
                        <FieldBody>
                            <Button Color="Color.Primary" Clicked="@(() => dataGridRef.ExpandGroups(_selectedGroupKey))">Expand Selected Group</Button>
                            <Button Color="Color.Secondary" Clicked="@(() => dataGridRef.CollapseGroups(_selectedGroupKey))">Collapse Selected Group</Button>
                        </FieldBody>
                    </Field>
                </Paragraph>

                <Paragraph>
                    <Field>
                        <FieldLabel>
                            <SelectList Data="@dataGridRef.DisplayGroupedData?.SelectMany(x=> x.NestedGroup)?.GroupBy(x=> x.Key)?.Select(x=> x.First())"
                                        TItem="GroupContext<Employee>" TValue="string"
                                        TextField="x=> x.Key" ValueField="x=> x.Key"
                                        @bind-SelectedValue="_selectedNestedGroupKey"></SelectList>
                        </FieldLabel>
                        <FieldBody>
                            <Button Color="Color.Primary" Clicked="@(() => dataGridRef.ExpandGroups(_selectedNestedGroupKey))">Expand Selected Group</Button>
                            <Button Color="Color.Secondary" Clicked="@(() => dataGridRef.CollapseGroups(_selectedNestedGroupKey))">Collapse Selected Group</Button>
                        </FieldBody>
                    </Field>
                </Paragraph>
            </CardBody>
            <CardBody>
                <DataGrid @ref="dataGridRef"
                          TItem="Employee"
                          Data="inMemoryData"
                          Responsive
                          ShowPager
                          ShowPageSizes
                          Groupable
                          ShowGrouping>
                    <DataGridColumns>
                        <DataGridColumn TextAlignment="TextAlignment.Center" TItem="Employee" Field="@nameof( Employee.Id )" Caption="#" Width="60px" />
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.FirstName )" Caption="First Name">
                        </DataGridColumn>
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.LastName )" Caption="Last Name" />
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.Email )" Caption="Email" />
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.City )" Caption="City">
                            <CaptionTemplate>
                                <Icon Name="IconName.City" /> @context.Caption
                            </CaptionTemplate>
                        </DataGridColumn>
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.Zip )" Caption="Zip">
                        </DataGridColumn>
                        <DataGridDateColumn TItem="Employee" Field="@nameof( Employee.DateOfBirth )" DisplayFormat="{0:dd.MM.yyyy}" Caption="Date Of Birth" Editable />
                        <DataGridNumericColumn TItem="Employee" Field="@nameof( Employee.Childrens )" Caption="Childrens" Editable Filterable="false" />
                        <DataGridSelectColumn TItem="Employee" Field="@nameof( Employee.Gender )" Caption="Gender" Editable Data="EmployeeData.Genders" ValueField="(x) => ((Gender)x).Code" TextField="(x) => ((Gender)x).Description" Groupable Grouping />
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.Salary )" Caption="Salary" Editable Width="140px" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" TextAlignment="TextAlignment.End">
                        </DataGridColumn>
                        <DataGridCheckColumn TItem="Employee" Field="@nameof(Employee.IsActive)" Caption="Active" Editable Filterable="false" Groupable Grouping>
                            <DisplayTemplate>
                                <Check TValue="bool" Checked="context.IsActive" Disabled ReadOnly />
                            </DisplayTemplate>
                        </DataGridCheckColumn>
                    </DataGridColumns>
                    <DataGridAggregates>
                        <DataGridAggregate TItem="Employee" Field="@nameof( Employee.Email )" Aggregate="DataGridAggregateType.Count">
                            <DisplayTemplate>
                                @($"Total emails: {context.Value}")
                            </DisplayTemplate>
                        </DataGridAggregate>
                        <DataGridAggregate TItem="Employee" Field="@nameof( Employee.Salary )" Aggregate="DataGridAggregateType.Sum" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" />
                        <DataGridAggregate TItem="Employee" Field="@nameof( Employee.IsActive )" Aggregate="DataGridAggregateType.TrueCount" />
                        <DataGridAggregate TItem="Employee" Field="@nameof( Employee.Childrens )" Aggregate="DataGridAggregateType.Sum" />
                    </DataGridAggregates>
                </DataGrid>
            </CardBody>
        </Card>
    </Column>
</Row>

@code {
    [Inject] EmployeeData EmployeeData { get; set; }

    private string _selectedGroupKey = "D";
    private string _selectedNestedGroupKey = "False";
    private DataGrid<Employee> dataGridRef;
    private IEnumerable<Employee> inMemoryData;

    protected override async Task OnInitializedAsync()
    {
        inMemoryData = ( await EmployeeData.GetDataAsync().ConfigureAwait( false ) ).Take( 25 );

        await base.OnInitializedAsync();
    }
}